#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <iostream>
#include <math.h>

#include "ShortWindowBlock.h"
#include "imageloader.h"

// Material Properties
GLfloat swblock1_diffuse[] = {0.50, 0.50, 0.50, 1.0};
GLfloat swblock2_diffuse[] = {1.00, 1.00, 1.00, 1.0};

ShortWindowBlock::ShortWindowBlock(void) {
	shortWindowLength = 1.0;
	shortWindowHeight = 0.5;
	shortWindowWidth = 1.0;
	loadImage();
}

void ShortWindowBlock::loadImage()
{
	Image* image1 = loadBMP("textures\\greenMilitary.bmp");
	Image* image2 = loadBMP("textures\\desertMilitary.bmp");
	Image* image3 = loadBMP("textures\\shortGreenMilitaryWithWindow.bmp");
	Image* image4 = loadBMP("textures\\greenMilitaryWithWindow.bmp");
	Image* image5 = loadBMP("textures\\shortDesertMilitaryWithWindow.bmp");
	Image* image6 = loadBMP("textures\\desertMilitaryWithWindow.bmp");
	greenMilitaryTexture = loadTexture(image1);
	desertMilitaryTexture = loadTexture(image2);
	shortGreenMilitaryWindowTexture = loadTexture(image3);
	greenMilitaryWindowTexture = loadTexture(image4);
	shortDesertMilitaryWindowTexture = loadTexture(image5);
	desertMilitaryWindowTexture = loadTexture(image6);
	delete image1;
	delete image2;
	delete image3;
	delete image4;
	delete image5;
	delete image6;
}

void ShortWindowBlock::display() {
	glPushMatrix();
		glPushMatrix();
			//half block
			glScalef(shortWindowLength, shortWindowHeight, shortWindowWidth);
			glMaterialfv(GL_FRONT, GL_DIFFUSE, swblock1_diffuse);
			glColor3f(0.5, 0.5, 0.5);
			glutSolidCube(1.0);
		glPopMatrix();

		float windowLength = 0.10;
		float windowHeight = 0.10;
		float windowSpace = 0.10;

		for(int i = 0; i != 3; ++i) {
			glPushMatrix();
				glTranslatef( (windowLength + windowSpace)*i, 0, 0.01);
				glTranslatef(-shortWindowLength/4, shortWindowHeight/10, shortWindowWidth/2);
				//quads for small windows above door
				glBegin(GL_QUADS);
					glMaterialfv(GL_FRONT, GL_DIFFUSE, swblock2_diffuse);
					glColor3f(0.0, 0.0, 0.0);
					glVertex3f(0.0, 0.0, 0.0);
					glVertex3f(windowLength, 0.0, 0.0);
					glVertex3f(windowLength, -windowHeight, 0.0);
					glVertex3f(0.0, -windowHeight, 0.0);
				glEnd();
			glPopMatrix();
		}
	glPopMatrix();
}

void ShortWindowBlock::greenMilitaryDisplay()
{
	glPushMatrix();
		//half block
		glScalef(shortWindowLength, shortWindowHeight, shortWindowWidth);
		glMaterialfv(GL_FRONT, GL_DIFFUSE, swblock1_diffuse);
		glColor3f(0.5, 0.5, 0.5);
		greenCube();
	glPopMatrix();
}

void ShortWindowBlock::greenMilitaryDisplay(int count)
{
	if (count == 3)
	{
		glPushMatrix();
			//half block
			glScalef(shortWindowLength, shortWindowHeight, shortWindowWidth);
			glMaterialfv(GL_FRONT, GL_DIFFUSE, swblock1_diffuse);
			glColor3f(0.5, 0.5, 0.5);
			greenCubeAlt();
		glPopMatrix();	
	}
	else
	{
		glPushMatrix();
			//half block
			glScalef(shortWindowLength, shortWindowHeight, shortWindowWidth);
			glMaterialfv(GL_FRONT, GL_DIFFUSE, swblock1_diffuse);
			glColor3f(0.5, 0.5, 0.5);
			greenCube();
		glPopMatrix();
	}
}

void ShortWindowBlock::desertMilitaryDisplay()
{
	glPushMatrix();
		//half block
		glScalef(shortWindowLength, shortWindowHeight, shortWindowWidth);
		glMaterialfv(GL_FRONT, GL_DIFFUSE, swblock1_diffuse);
		glColor3f(0.5, 0.5, 0.5);
		desertCube();
	glPopMatrix();
}

void ShortWindowBlock::greenCube()
{
	// vertices
	GLfloat A[] = { 0.0f, 1.0f, 1.0f }; 
	GLfloat B[] = { 1.0f, 1.0f, 1.0f }; 
	GLfloat C[] = { 1.0f, 1.0f, 0.0f }; 
	GLfloat D[] = { 0.0f, 1.0f, 0.0f }; 
	GLfloat E[] = { 0.0f, 0.0f, 1.0f }; 
	GLfloat F[] = { 1.0f, 0.0f, 1.0f }; 
	GLfloat G[] = { 1.0f, 0.0f, 0.0f }; 
	GLfloat H[] = { 0.0f, 0.0f, 0.0f }; 

	// texture coordinates
	GLfloat bottomLeft[] = {0.0f, 0.0f};
	GLfloat bottomRight[] = {1.0f, 0.0f};
	GLfloat topLeft[] = {0.0f, 1.0f};
	GLfloat topRight[] = {1.0f, 1.0f};


	glPushMatrix();

		glTranslatef(-0.5, -0.5, -0.5);

		glPushMatrix();

			glBindTexture(GL_TEXTURE_2D, greenMilitaryWindowTexture);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			// top
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 1.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(A);
				glTexCoord2fv(bottomRight);
				glVertex3fv(B);
				glTexCoord2fv(topRight);
				glVertex3fv(C);
				glTexCoord2fv(topLeft);
				glVertex3fv(D);
			glEnd();

			// bottom
			glBegin(GL_QUADS);
				glNormal3f( 0.0, -1.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(H);
				glTexCoord2fv(bottomRight);
				glVertex3fv(G);
				glTexCoord2fv(topRight);
				glVertex3fv(F);
				glTexCoord2fv(topLeft);
				glVertex3fv(E);
			glEnd();
		glPopMatrix();

		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D, shortGreenMilitaryWindowTexture);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		
			// Front
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 0.0, 1.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(E);
				glTexCoord2fv(bottomRight);
				glVertex3fv(F);
				glTexCoord2fv(topRight);
				glVertex3fv(B);
				glTexCoord2fv(topLeft);
				glVertex3fv(A);
			glEnd();

			// back
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 0.0, -1.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(G);
				glTexCoord2fv(bottomRight);
				glVertex3fv(H);
				glTexCoord2fv(topRight);
				glVertex3fv(D);
				glTexCoord2fv(topLeft);
				glVertex3fv(C);
			glEnd();

			// left
			glBegin(GL_QUADS);
				glNormal3f( -1.0, 0.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(H);
				glTexCoord2fv(bottomRight);
				glVertex3fv(E);
				glTexCoord2fv(topRight);
				glVertex3fv(A);
				glTexCoord2fv(topLeft);
				glVertex3fv(D);
			glEnd();

			// right
			glBegin(GL_QUADS);
				glNormal3f( 1.0, 0.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(F);
				glTexCoord2fv(bottomRight);
				glVertex3fv(G);
				glTexCoord2fv(topRight);
				glVertex3fv(C);
				glTexCoord2fv(topLeft);
				glVertex3fv(B);
			glEnd();

		glPopMatrix();

	glPopMatrix();
}

void ShortWindowBlock::greenCubeAlt()
{
	// vertices
	GLfloat A[] = { 0.0f, 1.0f, 1.0f }; 
	GLfloat B[] = { 1.0f, 1.0f, 1.0f }; 
	GLfloat C[] = { 1.0f, 1.0f, 0.0f }; 
	GLfloat D[] = { 0.0f, 1.0f, 0.0f }; 
	GLfloat E[] = { 0.0f, 0.0f, 1.0f }; 
	GLfloat F[] = { 1.0f, 0.0f, 1.0f }; 
	GLfloat G[] = { 1.0f, 0.0f, 0.0f }; 
	GLfloat H[] = { 0.0f, 0.0f, 0.0f }; 

	// texture coordinates
	GLfloat bottomLeft[] = {0.0f, 0.0f};
	GLfloat bottomRight[] = {1.0f, 0.0f};
	GLfloat topLeft[] = {0.0f, 1.0f};
	GLfloat topRight[] = {1.0f, 1.0f};


	glPushMatrix();

		glTranslatef(-0.5, -0.5, -0.5);

		glPushMatrix();

			glBindTexture(GL_TEXTURE_2D, greenMilitaryTexture);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			// top
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 1.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(A);
				glTexCoord2fv(bottomRight);
				glVertex3fv(B);
				glTexCoord2fv(topRight);
				glVertex3fv(C);
				glTexCoord2fv(topLeft);
				glVertex3fv(D);
			glEnd();

			// bottom
			glBegin(GL_QUADS);
				glNormal3f( 0.0, -1.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(H);
				glTexCoord2fv(bottomRight);
				glVertex3fv(G);
				glTexCoord2fv(topRight);
				glVertex3fv(F);
				glTexCoord2fv(topLeft);
				glVertex3fv(E);
			glEnd();
		glPopMatrix();

		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D, shortGreenMilitaryWindowTexture);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		
			// Front
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 0.0, 1.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(E);
				glTexCoord2fv(bottomRight);
				glVertex3fv(F);
				glTexCoord2fv(topRight);
				glVertex3fv(B);
				glTexCoord2fv(topLeft);
				glVertex3fv(A);
			glEnd();

			// back
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 0.0, -1.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(G);
				glTexCoord2fv(bottomRight);
				glVertex3fv(H);
				glTexCoord2fv(topRight);
				glVertex3fv(D);
				glTexCoord2fv(topLeft);
				glVertex3fv(C);
			glEnd();

			// left
			glBegin(GL_QUADS);
				glNormal3f( -1.0, 0.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(H);
				glTexCoord2fv(bottomRight);
				glVertex3fv(E);
				glTexCoord2fv(topRight);
				glVertex3fv(A);
				glTexCoord2fv(topLeft);
				glVertex3fv(D);
			glEnd();

			// right
			glBegin(GL_QUADS);
				glNormal3f( 1.0, 0.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(F);
				glTexCoord2fv(bottomRight);
				glVertex3fv(G);
				glTexCoord2fv(topRight);
				glVertex3fv(C);
				glTexCoord2fv(topLeft);
				glVertex3fv(B);
			glEnd();

		glPopMatrix();

	glPopMatrix();
}

void ShortWindowBlock::desertCube()
{
	// vertices
	GLfloat A[] = { 0.0f, 1.0f, 1.0f }; 
	GLfloat B[] = { 1.0f, 1.0f, 1.0f }; 
	GLfloat C[] = { 1.0f, 1.0f, 0.0f }; 
	GLfloat D[] = { 0.0f, 1.0f, 0.0f }; 
	GLfloat E[] = { 0.0f, 0.0f, 1.0f }; 
	GLfloat F[] = { 1.0f, 0.0f, 1.0f }; 
	GLfloat G[] = { 1.0f, 0.0f, 0.0f }; 
	GLfloat H[] = { 0.0f, 0.0f, 0.0f }; 

	// texture coordinates
	GLfloat bottomLeft[] = {0.0f, 0.0f};
	GLfloat bottomRight[] = {1.0f, 0.0f};
	GLfloat topLeft[] = {0.0f, 1.0f};
	GLfloat topRight[] = {1.0f, 1.0f};


	glPushMatrix();

		glTranslatef(-0.5, -0.5, -0.5);

		glPushMatrix();

			glBindTexture(GL_TEXTURE_2D, desertMilitaryTexture);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			// top
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 1.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(A);
				glTexCoord2fv(bottomRight);
				glVertex3fv(B);
				glTexCoord2fv(topRight);
				glVertex3fv(C);
				glTexCoord2fv(topLeft);
				glVertex3fv(D);
			glEnd();

			// bottom
			glBegin(GL_QUADS);
				glNormal3f( 0.0, -1.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(H);
				glTexCoord2fv(bottomRight);
				glVertex3fv(G);
				glTexCoord2fv(topRight);
				glVertex3fv(F);
				glTexCoord2fv(topLeft);
				glVertex3fv(E);
			glEnd();
		glPopMatrix();

		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D, shortDesertMilitaryWindowTexture);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		
			// Front
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 0.0, 1.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(E);
				glTexCoord2fv(bottomRight);
				glVertex3fv(F);
				glTexCoord2fv(topRight);
				glVertex3fv(B);
				glTexCoord2fv(topLeft);
				glVertex3fv(A);
			glEnd();

			// back
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 0.0, -1.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(G);
				glTexCoord2fv(bottomRight);
				glVertex3fv(H);
				glTexCoord2fv(topRight);
				glVertex3fv(D);
				glTexCoord2fv(topLeft);
				glVertex3fv(C);
			glEnd();

			// left
			glBegin(GL_QUADS);
				glNormal3f( -1.0, 0.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(H);
				glTexCoord2fv(bottomRight);
				glVertex3fv(E);
				glTexCoord2fv(topRight);
				glVertex3fv(A);
				glTexCoord2fv(topLeft);
				glVertex3fv(D);
			glEnd();

			// right
			glBegin(GL_QUADS);
				glNormal3f( 1.0, 0.0, 0.0);
				glTexCoord2fv(bottomLeft);
				glVertex3fv(F);
				glTexCoord2fv(bottomRight);
				glVertex3fv(G);
				glTexCoord2fv(topRight);
				glVertex3fv(C);
				glTexCoord2fv(topLeft);
				glVertex3fv(B);
			glEnd();

		glPopMatrix();

	glPopMatrix();

}



ShortWindowBlock::~ShortWindowBlock(void) {}